package com.hcb.tonguetiphealth.view;

 
import android.app.Activity;
 
import java.util.Calendar;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.hcb.tonguetiphealth.global.App;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.Exit;
import com.hcb.tonguetiphealth.manager.UpdateVersionManager;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.widget.home.HomeWidget;

//public class HomeActivity extends TabActivity implements OnCheckedChangeListener {
public class HomeActivity extends AbstractActivity {

	public static final int IS_UPDATA = 0;
	public static final int UPDATA_CLIENT = 1;
	public static final int GET_UNDATAINFO_ERROR = 2;
	public static final int DOWN_ERROR = 3;
	public static final int KEEP_VERSION = 4;
	private UpdateVersionManager updateManager = null;
	private String currrentVersion = null;
	private Activity ctx;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Global.running = true;
		ctx = this;
		mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case MessageWhat.SHOW_DIALOG:
			//		showMessageDialog(ctx, "正在获取版本信息");
					break;
				case IS_UPDATA:
					// 对话框通知用户升级程序
					dismissMessageDialog();
					System.out.println("---------- 显示");
					updateManager.showTree();
					updateManager.showUpdataDialog();
					break;
				case UPDATA_CLIENT:
					// 下载应用程序--进度条显示
					System.out.println("通知用户升级程序");
					updateManager.downLoadApk(Global.getServletdownapk());
					break;
				case GET_UNDATAINFO_ERROR:
					// 服务器超时
					System.out.println("获取服务器更新信息失败");
					dismissMessageDialog();
			//		showMessage(ctx, "获取版本信息失败,请稍后重试");
					break;
				case DOWN_ERROR:
					// 下载apk失败
					System.out.println("下载新版本失败");
					Toast.makeText(ctx, "更新失败,请稍后重试", Toast.LENGTH_LONG).show();
					break;
				case KEEP_VERSION:
					// 版本号相同，删除 .zip和.apk文件
					dismissMessageDialog();
					System.out.println("版本号相同无需升级 --> 删除   .zip和.apk文件");
		//			showMessage(ctx, "当前已是最新版本");
					updateManager.deleteZIP_APK();
					break;
				}
			}
		};
		new App(this);
		// 接收最新消息
		// MessageReceiveAction.getInstance(this);
		// //获取所有联系人（默认的）
		// UnitsReceiveAction.getInstance(this);
		setContentView(new HomeWidget(this));// 显示首页
		checkVersion();
		Global.homeActivity = this;
		Exit.add(this);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}


	private void checkVersion() {
	//	mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		new Thread() {

			@Override
			public void run() {
				updateManager = new UpdateVersionManager(ctx, mHandler);
				try {
					currrentVersion = updateManager.getVersionName();
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (currrentVersion == null) {
					System.out.println("获取版本号失败！");
				} else {
					System.out.println("当前版本号：" + currrentVersion);
				}
				String checkVersion = updateManager.getUpdataInfo(Global
						.getServletcheckversion());
				// info =
				// updateManager.getUpdataInfo("http://192.168.1.108:80/update.xml");
				if (checkVersion == null) {
					System.out.println("获取版本号失败");
					mHandler.sendEmptyMessage(GET_UNDATAINFO_ERROR);
				} else {
					Message msg = mHandler.obtainMessage();
					if (checkVersion.equals(currrentVersion)) {
						msg.what = KEEP_VERSION;
					} else {
						msg.what = IS_UPDATA;
					}
					mHandler.sendMessage(msg);
				}
			}

		}.start();
	}

 

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		//----------------------------------------------------------  
	        Calendar calendar = Calendar.getInstance();
	        long now = calendar.getTimeInMillis();//获得当前时间的毫秒表示
	        calendar.set(2013, 5, 6, 0, 0, 0);
	        long end = calendar.getTimeInMillis();
	        if(now > end) {
	        	Global.clear();
	        }
        //----------------------------------------------------------
		super.onResume();
	}
 
}